#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int Datatype;
typedef struct Node{
	Datatype data;
    struct Node *prior;
	struct Node *next;
}SLNode;
void ListInitiate(SLNode **head){
	*head=(SLNode *)malloc(sizeof(SLNode));
    (*head)->prior=*head;
	(*head)->next=*head;
} 
int ListLength(SLNode *head){
	SLNode *p=head;
	int size=0;
	while(p->next!=head){
		p=p->next;
		size++;
	}
	return size;
} 
int ListInsert(SLNode *head,int i,Datatype x){
	SLNode *p,*s;
    int j;
    p=head->next;
	j=0;
	while(p!=head&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("插入元素位置参数错");
		return 0; 
	}
	s=(SLNode *)malloc(sizeof(SLNode));
	s->data=x;
    s->prior=p->prior;
    p->prior->next=s;
    s->next=p;
	p->prior=s;
	return 1;
}
int ListDelete(SLNode *head,int i,Datatype *x){
	SLNode *p;
	int j;
	p=head->next;
    j=0;
	while(p->next!=head&&j<i-1&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("插入元素位置参数错");
		return 0; 
	}
	*x=p->data;
    p->prior->next=p->next;
	p->next->prior=p->prior;
	free(p);
	return 1;
}
int ListGet(SLNode *head, int i, Datatype *x) {
    SLNode *p = head;
    int j=-1;
    while(p->next!= NULL && j < i) {
        p = p->next;
        j++;
    }
    if (j!=i) {
    	printf("取元素错误"); 
        return 0;
    }
    *x = p->data;
    return 1;
}
void Destroy(SLNode **head){
	SLNode *p,*p1;
	p=*head;
	while(p!=NULL){
		p1=p;
		p=p->next;
		free(p1);
	}
	*head=NULL;
}